package com.dlz.systemshop.service.impl;

import com.dlz.systemshop.mapper.MenuMapper;
import com.dlz.systemshop.mapper.UserMapper;
import com.dlz.systemshop.pojo.LogUser;
import com.dlz.systemshop.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 董熠涛
 * @date 2025/4/25 11:48
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        User user = userMapper.selectUserByUsername(username);
        //根据用户名查询权限信息
        if (user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<String> list = menuMapper.findMenuByUserId(user.getId());
//        还可以加权限
        log.info("权限信息：{}",list);
        log.info("用户信息：{}",user);
        return new LogUser(list, user);
    }
}
